from django.db import models
from apps.students.models import Student
from apps.classes.models import Class


# 定义学生班级关联模型类
class StudentClass(models.Model):
    student = models.ForeignKey(Student, on_delete=models.PROTECT, related_name='all_classes', verbose_name='学生')
    classes = models.ForeignKey(Class, on_delete=models.PROTECT, related_name='all_students', verbose_name='班级')

    class Meta:
        # 指明数据库表名
        db_table = 'tb_student_class'
        # 在admin站点中显示的名称
        verbose_name = '学员班级表'
        # 显示的复数名称
        verbose_name_plural = verbose_name
        # 定义联合唯一字段
        unique_together = ['student', 'classes']

    def __str__(self):
        """定义每个数据对象的显示信息"""
        return f"{self.classes.stage_no}-{self.student.name}"
